const { ButtonInteraction } = require('discord.js');
const DB = require('../../Structures/Schemas/suggestDB.js');

module.exports = {
  name: 'interactionCreate',

  /**
   *
   * @param {ButtonInteraction} interaction
   */
  async execute(interaction) {
    if (!interaction.isButton()) return;

    if (!interaction.member.permissions.has('ADMINISTRATOR'))
      return interaction.reply({
        content: 'You cannot use this button',
        ephemeral: true,
      });

    const { guildId, customId, message } = interaction;

    DB.findOne(
      { GuildID: guildId, MEssageID: message.id },
      async (err, data) => {
        if (err) throw err;
        if (!data)
          return interaction.reply({
            content: 'No data was found in the database',
            ephemeral: true,
          });

        const embed = message.embeds[0];
        if (!embed) return;

        switch (customId) {
          case 'suggest-accept':
            {
              embed.fields[2] = {
                name: 'Status',
                value: 'Accepted',
                inline: true,
              };
              message.edit({
                embeds: [embed.setColor('GREEN')],
                components: [],
              });
              interaction.reply({
                content: 'Suggestion Accepted',

                ephemeral: true,
              });
            }
            break;
          case 'suggest-decline':
            {
              embed.fields[2] = {
                name: 'Status',
                value: 'Declined',
                inline: true,
              };
              message.edit({ embeds: [embed.setColor('RED')], components: [] });
              interaction.reply({
                content: 'Suggestion Declined',
                ephemeral: true,
              });
            }
            break;
        }
      }
    );
  },
};
